package cn.yl.caner.web;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.yl.caner.domain.AccountData;
import cn.yl.caner.domain.FindData;
import cn.yl.caner.service.FindService;

public class FindServlet extends HttpServlet {
	//寻找帐号
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		String username = request.getParameter("find_username");
		System.out.println("servlet取值成功"+username);
		FindData  finddata = new FindData();
		finddata.setFind_username(username);
		FindService findservice = new FindService();
		AccountData findaccount = findservice.findAccount(finddata);
		if(findaccount !=null) {
			//找到所有需要的帐号信息，并放入到request域中（可能有点傻傻的做法）
			 boolean isFind = true;
			 String find_username =findaccount.getUsername();
			 String find_email=findaccount.getEmail();
			 String find_phone=findaccount.getPhone();
			 String find_birthday_year=findaccount.getBirthday_year();
			 String find_birthday_month=findaccount.getBirthday_month();
			 String find_birthday_day=findaccount.getBirthday_day();
			 String find_age=findaccount.getAge();
		     String find_sex=findaccount.getSex();
			 String find_self=findaccount.getSelf();
			 String find_birthday = find_birthday_year+"年"+find_birthday_month+"月"+find_birthday_day+"日";
			 request.setAttribute("username", find_username);
			 request.setAttribute("email", find_email);
			 request.setAttribute("phone", find_phone);
			 if(find_birthday_year.equals("--") && find_birthday_month.equals("--") && find_birthday_day.equals("--")) {
				 request.setAttribute("birthday", "");
			 }else {
				 request.setAttribute("birthday", find_birthday);
			 }
			 if(find_age.equals("--")) {
				 request.setAttribute("age", "");
			 }else {
				 request.setAttribute("age", find_age);
			 }
			 if(find_sex.equals("man")) {
				 request.setAttribute("sex", "男");
			 }else{
				 request.setAttribute("sex", "女");
			 }
			 request.setAttribute("self", find_self);
			 request.setAttribute("isFind", isFind);
			request.getRequestDispatcher("/account..jsp").forward(request, response);
		}else {
			request.getRequestDispatcher("/findfail.jsp").forward(request, response);
		}
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}